<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 站点图标 -->
    <link rel="icon" type="image/png" sizes="32x32" href="{{ url_for('static', filename='images/favction.png') }}">
    <link rel="icon" type="image/png" sizes="16x16" href="{{ url_for('static', filename='images/favction.png') }}">
    <title>用户中心 - SkitPanel 短剧管理面板</title>
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- User Center CSS -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/user_center.css') }}">
    
    <!-- 自定义Tailwind配置 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3b82f6',
                        secondary: '#10b981',
                        dark: '#1e293b',
                        light: '#f8fafc',
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    
    <!-- 自定义工具类 -->
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .card-shadow {
                box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
            }
            .input-focus {
                @apply focus:ring-2 focus:ring-primary/50 focus:border-primary transition-all duration-200;
            }
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen flex flex-col">
    <!-- 头部导航 -->
    {% extends 'base.html' %}

    {% block title %}用户中心 - SkitPanel 短剧管理面板{% endblock %}

    {% block content %}
        <div class="container mx-auto px-4 py-8">
            <!-- 主内容区域 -->
            <div class="max-w-4xl mx-auto">
                <!-- 个人信息区块 -->
                <div class="space-y-6">
                    <!-- 个人信息卡片 -->
                    <div id="personal-info" class="bg-white rounded-xl card-shadow p-6 overflow-hidden">
                        <h3 class="text-lg font-bold text-gray-800 mb-6 flex items-center">
                            <i class="fa fa-user-circle text-primary mr-2"></i>
                            个人信息
                        </h3>
                        
                        {% with messages = get_flashed_messages(with_categories=true) %}
                            {% if messages %}
                                {% for category, message in messages %}
                                    <div class="mb-4 p-3 rounded-lg {% if category == 'error' %}bg-red-50 text-red-700 border border-red-200{% else %}bg-green-50 text-green-700 border border-green-200{% endif %}">
                                        {{ message }}
                                    </div>
                                {% endfor %}
                            {% endif %}
                        {% endwith %}
                        
                        <div class="flex flex-col items-center justify-center mb-6">
                            <!-- 头像上传提示 -->
                            <div class="text-sm text-gray-500 mb-3">点击编辑图标更换头像</div>
                            <!-- 用户头像 -->
                            <div class="relative mb-4">
                                <img id="user-avatar" src="{{ current_user.avatar if current_user.avatar else url_for('static', filename='images/favction.png') }}" 
                                     alt="用户头像" 
                                     class="w-32 h-32 rounded-full object-cover border-4 border-primary/10 shadow-lg transition-transform duration-300 hover:scale-105">
                                <button id="change-avatar-btn" class="absolute bottom-0 right-0 bg-primary hover:bg-primary/90 text-white rounded-full p-3 shadow-md transition-all duration-200 hover:shadow-lg hover:scale-105">
                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                        <path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4zm2 6a1 1 0 011-1h6a1 1 0 110 2H7a1 1 0 01-1-1zm1 3a1 1 0 100 2h6a1 1 0 100-2H7z" clip-rule="evenodd" />
                                    </svg>
                                </button>
                                <input type="file" id="avatar-input" accept="image/*" class="hidden">
                            </div>
                            
                            <!-- 用户名和角色 -->
                            <div class="text-center mb-2">
                                <h4 class="text-xl font-bold text-gray-800">{{ current_user.username }}</h4>
                                <p class="text-sm text-gray-500">{% if current_user.role == 'admin' %}管理员{% else %}普通用户{% endif %}</p>
                            </div>
                        </div>
                        
                        <!-- 账户详细信息 -->
                        <div class="bg-gray-50 p-5 rounded-lg shadow-sm">
                            <h4 class="text-sm font-medium text-gray-500 mb-4">账户详情</h4>
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <div class="flex items-center">
                                    <div class="w-8 h-8 bg-primary/10 rounded-full flex items-center justify-center mr-3">
                                        <i class="fa fa-calendar-plus-o text-primary"></i>
                                    </div>
                                    <div>
                                        <p class="text-xs text-gray-500">创建时间</p>
                                        <p class="text-sm font-medium text-gray-800">{{ current_user.created_at }}</p>
                                    </div>
                                </div>
                                <div class="flex items-center">
                                    <div class="w-8 h-8 bg-primary/10 rounded-full flex items-center justify-center mr-3">
                                        <i class="fa fa-sign-in text-primary"></i>
                                    </div>
                                    <div>
                                        <p class="text-xs text-gray-500">最后登录</p>
                                        <p class="text-sm font-medium text-gray-800">{{ current_user.last_login if current_user.last_login else '从未登录' }}</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 账户操作卡片 -->
                    <div id="account-actions" class="bg-white rounded-xl card-shadow p-6 overflow-hidden">
                        <h3 class="text-lg font-bold text-gray-800 mb-4 flex items-center">
                            <i class="fa fa-cog text-primary mr-2"></i>
                            账户操作
                        </h3>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <a href="{{ url_for('change_password') }}" class="flex items-center justify-center p-4 bg-primary hover:bg-primary/90 text-white rounded-lg transition-all duration-200 shadow hover:shadow-md">
                                <i class="fa fa-key text-xl mr-3"></i>
                                <span class="font-medium">修改密码</span>
                            </a>
                            <button id="logout-button" class="w-full flex items-center justify-center p-4 bg-red-500 hover:bg-red-600 text-white rounded-lg transition-all duration-200 shadow hover:shadow-md">
                                <i class="fa fa-sign-out text-xl mr-3"></i>
                                <span class="font-medium">退出登录</span>
                            </button>
                        </div>
                    </div>
                </div>
                    
                    <!-- 用户管理卡片（仅管理员可见） -->
                    {% if current_user.role == 'admin' %}
                        <div id="user-management" class="bg-white rounded-xl card-shadow p-6 overflow-hidden mt-6">
                            <h3 class="text-lg font-bold text-gray-800 mb-4 flex items-center">
                                <i class="fa fa-users text-primary mr-2"></i>
                                用户管理
                            </h3>
                            
                            <!-- 添加用户按钮 -->
                            <div class="mb-4">
                                <button id="add-user-button" class="bg-primary hover:bg-primary/90 text-white font-medium py-2 px-4 rounded-lg transition-colors duration-200 flex items-center gap-1.5">
                                    <i class="fa fa-plus mr-1"></i> 添加用户
                                </button>
                            </div>
                            
                            <!-- 用户列表 -->
                            <div class="overflow-x-auto">
                                <table class="min-w-full divide-y divide-gray-200">
                                    <thead class="bg-gray-50">
                                        <tr>
                                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">用户名</th>
                                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">角色</th>
                                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">创建时间</th>
                                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">最后登录</th>
                                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody class="bg-white divide-y divide-gray-200">
                                        {% for user in all_users %}
                                            <tr>
                                                <td class="px-6 py-4 whitespace-nowrap">
                                                    <div class="font-medium text-gray-900">{{ user.username }}</div>
                                                </td>
                                                <td class="px-6 py-4 whitespace-nowrap">
                                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-blue-100 text-blue-800">
                                                        {% if user.role == 'admin' %}管理员{% else %}普通用户{% endif %}
                                                    </span>
                                                </td>
                                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                    {{ user.created_at }}
                                                </td>
                                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                    {{ user.last_login if user.last_login else '从未登录' }}
                                                </td>
                                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                                    {% if user.username != 'admin' %}
                                                        <button class="edit-user-button text-primary hover:text-primary/80 mr-3" data-username="{{ user.username }}">
                                                            <i class="fa fa-pencil mr-1"></i> 编辑
                                                        </button>
                                                        <button class="delete-user-button text-red-500 hover:text-red-600" data-username="{{ user.username }}">
                                                            <i class="fa fa-trash mr-1"></i> 删除
                                                        </button>
                                                    {% else %}
                                                        <span class="text-gray-400">不可编辑</span>
                                                    {% endif %}
                                                </td>
                                            </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    {% endif %}
            </div>
        </div>
        
        <!-- 添加/编辑用户模态框 -->
        <div id="user-modal" class="fixed inset-0 z-50 flex items-center justify-center hidden opacity-0 transition-opacity duration-300">
            <div class="absolute inset-0 bg-black bg-opacity-50" id="user-modal-overlay"></div>
            <div class="bg-white rounded-xl shadow-2xl max-w-md w-full max-h-[90vh] overflow-y-auto z-10 transform scale-95 transition-transform duration-300" id="user-modal-content">
                <div class="p-6 border-b">
                    <div class="flex justify-between items-center">
                        <h3 class="text-xl font-bold text-gray-800" id="user-modal-title">添加用户</h3>
                        <button id="close-user-modal" class="text-gray-500 hover:text-gray-700 transition-colors duration-200">
                            <i class="fa fa-times text-xl"></i>
                        </button>
                    </div>
                </div>
                <form id="user-form">
                    <input type="hidden" name="action" id="user-action" value="add">
                    <input type="hidden" name="username" id="user-modal-username" value="">
                    <div class="p-6">
                        <div class="mb-4">
                            <label for="new-username" class="block text-sm font-medium text-gray-700 mb-1">用户名</label>
                            <input type="text" id="new-username" name="new-username" required
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg shadow-sm input-focus" 
                                   placeholder="请输入用户名">
                        </div>
                        
                        <div id="password-field" class="mb-4">
                            <label for="new-password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
                            <div class="relative">
                                <input type="password" id="new-password" name="new-password"
                                       class="w-full pl-3 pr-10 py-2 border border-gray-300 rounded-lg shadow-sm input-focus" 
                                       placeholder="请输入密码">
                                <button type="button" id="toggle-new-password" class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-400 hover:text-gray-600 transition-colors duration-200">
                                    <i class="fa fa-eye-slash" aria-hidden="true"></i>
                                </button>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">密码至少包含8个字符，包括大小写字母、数字和特殊字符</p>
                        </div>
                        
                        <div class="mb-4">
                            <label for="user-role" class="block text-sm font-medium text-gray-700 mb-1">角色</label>
                            <select id="user-role" name="user-role" required
                                    class="w-full px-3 py-2 border border-gray-300 rounded-lg shadow-sm input-focus">
                                <option value="user">普通用户</option>
                                <option value="admin">管理员</option>
                            </select>
                        </div>
                    </div>
                    <div class="p-6 border-t bg-gray-50">
                        <div class="flex space-x-3">
                            <button type="submit" class="flex-1 bg-primary hover:bg-primary/90 text-white font-medium py-2 px-4 rounded-lg transition-colors duration-200">
                                <i class="fa fa-check mr-1"></i> 确认
                            </button>
                            <button type="button" id="cancel-user-modal" class="flex-1 bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium py-2 px-4 rounded-lg transition-colors duration-200">
                                <i class="fa fa-times mr-1"></i> 取消
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    {% endblock %}

    {% block scripts %}
        <!-- User Center JavaScript -->
        <script src="{{ url_for('static', filename='js/user_center.js') }}"></script>
    {% endblock %}
</body>
</html>